package com.justech.mes.utils.plugins.transfer;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author feifei.li
 */
public class DefaultProcesser extends AbstractProcesser implements Processer {
    @Override
    public <T> T process(T source) {
        return super.process(source);
    }


    @Override
    public List<Object> process(List<Object> sources) {
        sources.parallelStream().forEach(e -> process(e));
        return sources;
    }

    @Override
    public Object processSingleType(Object singleType, Annotation annotation) {
        return super.processSingleType(singleType, annotation);
    }

    @Override
    public Object processSingleType(Object singleType, Annotation[] annotations) {
        for (Annotation annotation : annotations) {
            singleType = processSingleType(singleType, annotation);
        }
        return singleType;
    }

    @Override
    public List<Object> processToList(Object source) {
        Class<?> sourceClass = source.getClass();
        Field[] fields = sourceClass.getDeclaredFields();
        ArrayList<Object> objectArrayList = new ArrayList<>();
        for (Field f : fields) {
            f.setAccessible(true);
            if (isMarked(f.getAnnotations())) {
                Object value = ReflexUtilSandBox.getValue(f, source);
                value = processSingleType(value, f.getAnnotations());
                objectArrayList.add(value);
            } else {
                continue;
            }
        }
        return objectArrayList;
    }
}
